$(document).ready(function(e) {
	var tables = $("table").not(".noAutoClass");
	tables
		.addClass("infoTable center")
		.attr({border:0})
	;
	
	tables.each(function(indextable, element) {
		var ti1=$(this);
		var trs = ti1.find("tbody>tr");
		trs.each(function(indextr, element) {
			var tr = $(this),tds=tr.children("td");
			var tdLength=0;
			//计算每行单元格的数量
			tds.each(function(index, element) {tdLength += (parseInt($(this).attr("colspan")) || 1);});
			tds.each(function(indextd, element) {
				var td=$(this);
				var colspanMount = td.attr("colspan");
				if(!colspanMount){															//非跨行td
					td.addClass(indextd%2?"itemtd":"labeltd");
				}else if(parseInt(colspanMount)==tdLength){									//跨行整个表格
					td.addClass("crossAll");
				}else{																		//跨行td暂不处理
					
				}
			});
			tdLength = 0;
		});
	});
	
	
	$("body").not(".noAutoClass").addClass("body");
	
	//fancyBox plus
	var targetIdToFancyButton={};
	var fancyButtons = $("*[fancyTo]");
	var hiddenBox=$("<div></div>",{id:"hiddenBox"}).css({display:"none"}).appendTo($("body"));
	fancyButtons.each(function(){
		var ti=$(this);
		var data=ti.data();
		var proxy = data.proxy=$("<a></a>");
		var targetId=ti.attr("fancyTo");
		var optionsString = ti.attr("fancyOptions");
		var opts = data.opts = {};
		if(optionsString){
			$.each(optionsString.split(";"),function(key1,item1){
				if(item1=="")	return;
				var item1Split=item1.split(":");
				if(item1Split.length==2){
					opts[item1Split[0]]=item1Split[1];
				}else{
					throw("fancyOptions属性不合法!"); 
				}
			});
		}
		
		//单击不打开（）
		var realFancyBox = proxy;//opts.cacheOpen=="true"?proxy:ti;
		realFancyBox.attr("href","#"+targetId);
		realFancyBox.fancybox({
			hideOnOverlayClick:false,
			padding:10,
			overlayOpacity:0.2 
		});
		targetIdToFancyButton[targetId]=ti;
		
		$("#"+targetId)
			.css({display:"",width:opts.width||650})
			.appendTo(hiddenBox)
		;
		
		if(opts.cacheOpen=="true"){
			ti.click($.fancybox.showActivity);
		}else{
			//ti.click($.fancybox.showActivity);
			ti.click(function(){$(this).data().proxy.trigger("click");});
		}
	});
	
	$(window).bind("EV_fancyOpen",function(e,targetId){
		var ti = targetIdToFancyButton[targetId];
		ti.data().proxy.trigger("click");
		$.fancybox.hideActivity();
	});
	$(window).bind("EV_fancyClose",function(e,targetId){
		$.fancybox.close();
	});
	
	$(window).bind("EV_fancyHideLoading",function(e){
		$.fancybox.hideActivity();
	});
	//--fancyBox plus
	
	//生成图标
	$.getIconImg=function(picname){
		var baseUrl = (basePath||"")+"/images/"+picname;
		return $("<img src='" + baseUrl + "'/>").css({marginBottom:-3});
	}
}); 


//证件号验证
var idnoVerifyMap={
	//idnoType	:	regexp
	"default"	:	/^[0-9a-zA-Z]{4,21}$/,
	"1"			:	/^\d{1,5}$/,
	"2"			:	/^\d{1,5}$/,
	"4"			:	/^\w{1,5}$/
}


//根据idTypeName模糊识别正则
var patterns={
	"身份"	:	/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/,
	"军官"	:  	/^\d{4,15}$/
}

//证件类型加载完毕
$(window).bind("onIdtypeGot",function(e,data){
	$.each(data,function(i,ele){
		$.each(patterns,function(kk,elem){
			if(ele.idTypeName.indexOf(kk)!=-1){
				idnoVerifyMap[ele.idTypeId] = elem;
			}
		});
	});
});

var idnoVerify={
	"idno":recheckWhenFunc
}

//通用方法
function recheckWhenFunc(gets,obj,curform,regxp){
	var recheckWhenVal,recheckWhenItem,reg;
	if(obj.attr("recheckWhen")){
		recheckWhenItem = obj.data().recheckWhen || (obj.data().recheckWhen = curform.find("#"+obj.attr("recheckWhen")));
		recheckWhenVal = recheckWhenItem.val();
		reg = idnoVerifyMap[recheckWhenVal];
		if(!recheckWhenItem.data().binded){
			recheckWhenItem.data().binded=true;
			recheckWhenItem.bind("change",function(e){
				obj.trigger("blur");
			});
		}
	}
	
	reg = reg || idnoVerifyMap["default"];
	return reg.test(gets);
}

var validFormConfig = {
	tiptype_img		:	function(msg, obj, cssctl, isWhenBlur) {
		var msgspan = obj.obj.parent().children(".xinghao");
		if(msgspan.length==0){
			msgspan = $("<span></span>",{"class":"xinghao"}).appendTo(obj.obj.parent());
		}
		if (obj.type != 2) {
			msgspan.text(msg);
		} else {
			msgspan.text("*");
			msgspan.empty().append($.getIconImg("ok.png"));
		}
	}
}

//获取滚动条的宽度
function getScrollWidth(isRecalculate) {                    //是否重新计算。否则从缓存里取
    if(getScrollWidth.cacheSwidth == undefined || isRecalculate){
        var noScroll, scroll, oDiv = document.createElement("DIV");
        oDiv.style.cssText = "position:absolute; top:-1000px; width:100px; height:100px; overflow:hidden;";
        noScroll = document.body.appendChild(oDiv).clientWidth;
        oDiv.style.overflowY = "scroll";
        scroll = oDiv.clientWidth;
        document.body.removeChild(oDiv);
        getScrollWidth.cacheSwidth = noScroll-scroll;
    }
    return getScrollWidth.cacheSwidth;
}





